package com.flashcash.solu.view.me;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flashcash.kotlin.data.LoanStatus;
import com.flashcash.kotlin.widget.LocalButton;
import com.flashcash.solu.R;
import com.flashcash.solu.bean.HistoryLoanAppInfoBean;
import com.flashcash.solu.common.FieldParams;
import com.flashcash.solu.utils.CommonUtils;
import com.flashcash.solu.utils.StringFormatUtils;


/**
 * @作者:XJY
 * @创建日期: 2017/3/15 11:49
 * @描述:MyLoan功能模块Adapter的ViewHolder
 * @更新者:${Author}$
 * @更新时间:${Date}$
 * @更新描述:${TODO}
 */
public class MyLoanViewHolder extends RecyclerView.ViewHolder {

    TextView mAmount;
    TextView mStatus;
    TextView mTime;
    TextView mRepaymentTime;
    TextView mRppId;
    TextView mInterest;
    TextView mServiceFee;
    TextView mTotalRepayment;
    TextView mReceiptAmount;
    TextView mWithdrawOpt;
    TextView mControlNumber;
    TextView mAppId;
    TextView mContract;
    TextView mSender;
    TextView mBankName;
    TextView mBankcardNumber;
    View mLine;
    RelativeLayout mRlBtn;
    LocalButton mCancel;
    LocalButton mDelete;
    LinearLayout mLlIdNumber;
    LinearLayout mLlInterest;
    LinearLayout mLlServiceFee;
    LinearLayout mLlTotalRepayment;
    LinearLayout mLlReceiptAmount;
    LinearLayout mLlIWithdrawOpt;
    LinearLayout mLlControlNumber;
    LinearLayout mLlAppId;
    LinearLayout mLlContractId;
    LinearLayout mLlSenderId;
    LinearLayout mLlBankNameId;
    LinearLayout mLlBankcardNumberId;
    ImageView mArrow;

    public MyLoanViewHolder(View itemView) {
        super(itemView);
        mRppId = itemView.findViewById(R.id.tv_item_myloan_id);
        mAmount = itemView.findViewById(R.id.tv_item_myloan_loanamount);
        mStatus = itemView.findViewById(R.id.tv_item_myloan_status);
        mTime = itemView.findViewById(R.id.tv_item_myloan_time);
        mRepaymentTime = itemView.findViewById(R.id.tv_item_myloan_repayment_time);
        mInterest = itemView.findViewById(R.id.tv_item_myloan_interest);
        mServiceFee = itemView.findViewById(R.id.tv_item_myloan_service_fee);
        mTotalRepayment = itemView.findViewById(R.id.tv_item_myloan_total_repayment);
        mReceiptAmount = itemView.findViewById(R.id.tv_item_myloan_receipt_amount);
        mWithdrawOpt = itemView.findViewById(R.id.tv_item_myloan_withdraw_opt);
        mControlNumber = itemView.findViewById(R.id.tv_item_myloan_control_number);
        mAppId = itemView.findViewById(R.id.tv_item_app_id);
        mLine = itemView.findViewById(R.id.line);
        mRlBtn = itemView.findViewById(R.id.rl_btn);
        mCancel = itemView.findViewById(R.id.btn_cancel);
        mDelete = itemView.findViewById(R.id.btn_delete);

        mLlIdNumber = itemView.findViewById(R.id.ll_id_number);
        mLlInterest = itemView.findViewById(R.id.ll_id_interest);
        mLlServiceFee = itemView.findViewById(R.id.ll_id_service_fee);
        mLlTotalRepayment = itemView.findViewById(R.id.ll_id_total_repayment);
        mLlReceiptAmount = itemView.findViewById(R.id.ll_id_receipt_amount);
        mLlIWithdrawOpt = itemView.findViewById(R.id.ll_id_withdraw_opt);
        mLlControlNumber = itemView.findViewById(R.id.ll_id_control_number);
        mLlAppId = itemView.findViewById(R.id.ll_app_id);
        mArrow = itemView.findViewById(R.id.iv_arrow);

        mContract = itemView.findViewById(R.id.tv_item_myloan_contract_number);
        mSender = itemView.findViewById(R.id.tv_item_myloan_sender);
        mBankName = itemView.findViewById(R.id.tv_item_myloan_bank_name);
        mBankcardNumber = itemView.findViewById(R.id.tv_item_myloan_bankcard_number);

        mLlContractId = itemView.findViewById(R.id.ll_id_contract_number);
        mLlSenderId = itemView.findViewById(R.id.ll_id_sender);
        mLlBankNameId = itemView.findViewById(R.id.ll_id_bank_name);
        mLlBankcardNumberId = itemView.findViewById(R.id.ll_id_bankcard_number);
    }

    public void injectView(final Context ctx, final HistoryLoanAppInfoBean historyLoanAppInfoBean) {
        mTime.setText(CommonUtils.convertEnglishTime(historyLoanAppInfoBean.getCreateTime()));
        mAmount.setText("PHP " + StringFormatUtils.moneyFormatWithSplitWithUnitWithoutDecimal(historyLoanAppInfoBean.getAmount(), ctx));
        mRepaymentTime.setText(StringFormatUtils.dayTimeFormat(ctx, historyLoanAppInfoBean.getPeriod()));
        final String status = historyLoanAppInfoBean.getStatus();
        int statusStringRes = LoanStatus.Companion.getStringRes(status);
        if (statusStringRes != -1) {
            mStatus.setText(statusStringRes);
        } else {
            mStatus.setText(status);
        }
        mArrow.setSelected(historyLoanAppInfoBean.isExpand());
        /*if (TextUtils.equals(FieldParams.LoanStatus.REJECTED, status) || TextUtils.equals
                (FieldParams.LoanStatus.WITHDRAWN, status) || TextUtils.equals(FieldParams
                .LoanStatus.SUBMITTED, status) ||
                TextUtils.equals(FieldParams.LoanStatus.PAID_OFF, status) || TextUtils.equals
                (FieldParams.LoanStatus.CLOSED, status)) {
            mStatus.setTextColor(ctx.getResources().getColor(R.color.my_loan_status_black));
        } else {
            mStatus.setTextColor(ctx.getResources().getColor(R.color.my_loan_status_purple));
        }*/
//        1、先判断是否为展开状态
        if (historyLoanAppInfoBean.isExpand()) {//展开
            mLlIdNumber.setVisibility(View.VISIBLE);
            mLlInterest.setVisibility(View.VISIBLE);
            mLlServiceFee.setVisibility(View.VISIBLE);
            mLlTotalRepayment.setVisibility(View.VISIBLE);
            mLlReceiptAmount.setVisibility(View.VISIBLE);
            mLlAppId.setVisibility(View.VISIBLE);

            mRppId.setText(historyLoanAppInfoBean.getCredentialNo());
            mInterest.setText("PHP " + StringFormatUtils.moneyFormatWithSplitWithUnitWithoutDecimal(historyLoanAppInfoBean.getInterestAccr(), ctx));
            mServiceFee.setText("PHP " + StringFormatUtils.moneyFormatWithSplitWithUnitWithoutDecimal(historyLoanAppInfoBean.getServiceFeeAccr(), ctx));
            mTotalRepayment.setText("PHP " + StringFormatUtils.moneyFormatWithSplitWithUnitWithoutDecimal(historyLoanAppInfoBean.getDueAmount(), ctx));
            mReceiptAmount.setText("PHP " + StringFormatUtils.moneyFormatWithSplitWithUnitWithoutDecimal(historyLoanAppInfoBean.getIssueAmount(), ctx));
            mAppId.setText(historyLoanAppInfoBean.getLoanAppId());

            //2 判断状态
            if (TextUtils.equals(FieldParams.LoanStatus.WITHDRAWN, status)
                    || TextUtils.equals(FieldParams.LoanStatus.ISSUE_FAILED, status)
                    || TextUtils.equals(FieldParams.LoanStatus.PAID_OFF, status)
                    || TextUtils.equals(FieldParams.LoanStatus.REJECTED, status)
                    || TextUtils.equals(FieldParams.LoanStatus.CURRENT, status)
                    || TextUtils.equals(FieldParams.LoanStatus.OVERDUE, status)
                    || TextUtils.equals(FieldParams.LoanStatus.ISSUING, status)
                    || TextUtils.equals(FieldParams.LoanStatus.GRACE_PERIOD, status)
                    || TextUtils.equals(FieldParams.LoanStatus.APPROVED, status)
            ) {//显示删除按钮,暂时不显示删除按钮
                mLine.setVisibility(View.GONE);
                mRlBtn.setVisibility(View.GONE);
                mCancel.setVisibility(View.GONE);
                mDelete.setVisibility(View.GONE);
                /*mDelete.setOnClickListener(v -> {
                    if (callBack != null) {
                        callBack.onDeleteClick(historyLoanAppInfoBean);
                    }
                });*/
            } else {//显示取消按钮
                mLine.setVisibility(View.VISIBLE);
                mRlBtn.setVisibility(View.VISIBLE);
                mCancel.setVisibility(View.VISIBLE);
                mDelete.setVisibility(View.GONE);
                mCancel.setOnClickListener(v -> {
                    if (callBack != null) {
                        callBack.onCancelClick(historyLoanAppInfoBean);
                    }
                });
            }

            //空值不显示
            if (TextUtils.isEmpty(historyLoanAppInfoBean.getMerchantName())) {
                mLlIWithdrawOpt.setVisibility(View.GONE);
            } else {
                mLlIWithdrawOpt.setVisibility(View.VISIBLE);
                mWithdrawOpt.setVisibility(View.VISIBLE);
                mWithdrawOpt.setText(historyLoanAppInfoBean.getMerchantName());
            }
            if (TextUtils.isEmpty(historyLoanAppInfoBean.getControlNumber())) {
                mLlControlNumber.setVisibility(View.GONE);
            } else {
                mLlControlNumber.setVisibility(View.VISIBLE);
                mControlNumber.setVisibility(View.VISIBLE);
                mControlNumber.setText(historyLoanAppInfoBean.getControlNumber());
            }
            if (TextUtils.isEmpty(historyLoanAppInfoBean.getContractNumber())) {
                mLlContractId.setVisibility(View.GONE);
            } else {
                mLlContractId.setVisibility(View.VISIBLE);
                mContract.setVisibility(View.VISIBLE);
                mContract.setText(historyLoanAppInfoBean.getContractNumber());
            }
            if (TextUtils.isEmpty(historyLoanAppInfoBean.getSender())) {
                mLlSenderId.setVisibility(View.GONE);
            } else {
                mLlSenderId.setVisibility(View.VISIBLE);
                mSender.setVisibility(View.VISIBLE);
                mSender.setText(historyLoanAppInfoBean.getSender());
            }
            if (TextUtils.isEmpty(historyLoanAppInfoBean.getBankCode())) {
                mLlBankNameId.setVisibility(View.GONE);
            } else {
                mLlBankNameId.setVisibility(View.VISIBLE);
                mBankName.setVisibility(View.VISIBLE);
                mBankName.setText(historyLoanAppInfoBean.getBankCode());
            }
            if (TextUtils.isEmpty(historyLoanAppInfoBean.getCardNo())) {
                mLlBankcardNumberId.setVisibility(View.GONE);
            } else {
                mLlBankcardNumberId.setVisibility(View.VISIBLE);
                mBankcardNumber.setVisibility(View.VISIBLE);
                mBankcardNumber.setText(historyLoanAppInfoBean.getCardNo());
            }
        } else {//合并
            mLlIdNumber.setVisibility(View.GONE);
            mLlInterest.setVisibility(View.GONE);
            mLlServiceFee.setVisibility(View.GONE);
            mLlTotalRepayment.setVisibility(View.GONE);
            mLlReceiptAmount.setVisibility(View.GONE);
            mLlAppId.setVisibility(View.GONE);
            mLine.setVisibility(View.GONE);
            mRlBtn.setVisibility(View.GONE);
            mCancel.setVisibility(View.GONE);
            mDelete.setVisibility(View.GONE);
            mLlIWithdrawOpt.setVisibility(View.GONE);
            mWithdrawOpt.setVisibility(View.GONE);
            mLlControlNumber.setVisibility(View.GONE);
            mControlNumber.setVisibility(View.GONE);
            mLlContractId.setVisibility(View.GONE);
            mContract.setVisibility(View.GONE);
            mLlSenderId.setVisibility(View.GONE);
            mSender.setVisibility(View.GONE);
            mLlBankNameId.setVisibility(View.GONE);
            mBankName.setVisibility(View.GONE);
            mLlBankcardNumberId.setVisibility(View.GONE);
            mBankcardNumber.setVisibility(View.GONE);
        }

        //按钮点击状态
        itemView.setOnClickListener(v -> {
//                if (TextUtils.equals(FieldParams.LoanStatus.WITHDRAWN,status)) {
//                    XLeoToast.showMessage(ctx.getString(R.string.show_already_colosed));
//                }else if(TextUtils.equals(FieldParams.LoanStatus.SUBMITTED,status)){
//                    XLeoToast.showMessage(ctx.getString(R.string.show_submitted));
//                }else if(TextUtils.equals(FieldParams.LoanStatus.PAID_OFF,status)){
//                    XLeoToast.showMessage(ctx.getString(R.string.show_paid_off));
//                }else{
//                    TokenManager instance = TokenManager.newInstance();
//                    instance.storeMessage(FieldParams.SHOW_MY_LOAN_INFO,true);
//                    instance.storeMessage(FieldParams.LOAN_APP_INFO_BEAN,historyLoanAppInfoBean);
//                    if (ctx instanceof Activity) {
//                        ((Activity) ctx).finish();
//                    }else{
//                        ctx.startActivity(new Intent(ctx, MainActivity.class));
//                    }
//                }
            // TokenManager.putMessage(FieldParams.SHOW_MY_LOAN_INFO,historyLoanAppInfoBean);
            /*CacheDataManage.INSTANCE.addData(FieldParamsInt.SHOW_MY_LOAN_INFO,
                    historyLoanAppInfoBean);
            ctx.startActivity(new Intent(ctx, LoanInfoActivity.class));*/
//            historyLoanAppInfoBean.setExpand(!historyLoanAppInfoBean.isExpand());
            if (callBack != null) {
                //更改状态
                callBack.onItemClick(historyLoanAppInfoBean);
            }
        });
    }

    interface CallBack {
        void onItemClick(HistoryLoanAppInfoBean historyLoanAppInfoBean);

        void onCancelClick(HistoryLoanAppInfoBean historyLoanAppInfoBean);

        void onDeleteClick(HistoryLoanAppInfoBean historyLoanAppInfoBean);
    }

    private CallBack callBack;

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }
}
